// 点赞与取消点赞
<template>
  <van-button
    :icon="this.value ? 'good-job' : 'good-job-o'"
    :class="{ liked: value === 1 }"
    style="border:none"
    @click="onLike"
  ></van-button>
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'

export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true,
    },
    art_id: {
      type: [Number, String],
      required: true,
    },
  },
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLike() {
      try {
        //   如果点赞，就取消点赞
        if (this.value === 1) {
          await deleteLike(this.art_id)
        } else {
          // 未点赞 添加点赞
          await addLike(this.art_id)
        }
        this.$toast.success(this.value === 1 ? '取消点赞成功' : '点赞成功')
        this.$emit('input', this.value === 1 ? -1 : 1)
      } catch (err) {
        this.$toast.fail('点赞失败')
      }
    },
  },
}
</script>

<style lang="less" scoped>
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
